package com.tdr.comicbook.db;

/**
 * Created by lemingdeng on 2014-06-25.
 */
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_MYBOOKS = "myBooks";
    public static final String COLUMN_BOOKNAME = "bookName";
    public static final String COLUMN_BOOKPATH = "bookPath";
    public static final String COLUMN_BOOKCOVER = "bookCover";

    public static final String TABLE_SETTINGS = "settings";
    public static final String COLUMN_KEY = "key";
    public static final String COLUMN_VALUE = "value";

    private static final String DATABASE_NAME = "ComicBook.db";
    private static final int DATABASE_VERSION = 1;

    private static MySQLiteHelper mInstance = null;


    public static MySQLiteHelper getInstance(Context ctx) {
        /**
         * use the application context as suggested by CommonsWare.
         * this will ensure that you dont accidentally leak an Activitys
         * context (see this article for more information:
         * http://developer.android.com/resources/articles/avoiding-memory-leaks.html)
         */
        if (mInstance == null) {
            mInstance = new MySQLiteHelper(ctx.getApplicationContext());
        }
        return mInstance;
    }
    // Database creation sql statement
    private static final String MYBOOKS_CREATE = "create table "
            + TABLE_MYBOOKS + "(" + COLUMN_BOOKNAME
            + " varchar primary key, " + COLUMN_BOOKPATH
            + " varchar not null, "  + COLUMN_BOOKCOVER
            + " varchar not null);";

    private static final String SETTINGS_CREATE = "create table "
            + TABLE_SETTINGS + "(" + COLUMN_KEY
            + " varchar primary key, " + COLUMN_VALUE
            + " varchar);";

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(MYBOOKS_CREATE);
        db.execSQL(SETTINGS_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MYBOOKS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);
        onCreate(db);
    }

}

